var id =getQueryString('id')
var url='../inspectionplan/list';
if(id){
    url='../inspectionplan/list?id='+id
}
$(function () {
    $("#jqGrid").Grid({
        url: url ,
        colModel: [
            /*   {label: '是否删除(1:未删除，0：删除)', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '巡检名称', name: 'planName', index: 'PLAN_NAME', width: 80},
            {label: '执行人', name: 'executiveName', index: 'EXECUTIVE', width: 80},
            {label: '车间', name: 'workshopName', index: 'WORKSHOP', width: 80},
            {label: '生产线', name: 'productionLineName', index: 'PRODUCTION_LINE', width: 80},
            {
                label: '执行时间', name: 'carryDate', index: 'CARRY_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss')
                }
            },
            {
                label: '完成时间', name: 'fulfilDate', index: 'FULFIL_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss')
                }
            },
            {
                label: '关闭时间', name: 'endDate', index: 'END_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss')
                }
            },
            {label: '巡检选项', name: 'option',  sortable: false, align: 'center',
                formatter:function(value, col, row){
                    return "<a onclick='vm.showOptionBase(\"" + row.id + "\")'>关联选项</a>";
            }},
        ]
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        },
    },
    callback: {
        beforeCheck: true,
    }

};
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        inspectionPlan: {
            delFlag: '',
            endDate: '',
            fulfilDate: '',
            carryDate: '',
            productionLine: '',
            productionLineName: '',
            workshop: '',
            workshopName: '',
            executive: '',
            planName: '',
            id: '',
            devCode: '',
            devName: '',
            devId: '',
        },
        ruleValidate: {
            planName: [
                {required: true, message: '计划名称不能为空', trigger: 'blur'}
            ],
            devCode: [
                {required: true, message: '关联维护规程不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        assignmentPlanData: {
            name:'',
            executive: '',
            codeId: '',
            remark: ''
        },
        assignmentPlanVal:{},
        executiveList: {},
        data1:{}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            // var ids = getDeptId();
            // if (ids == null) {
            //     alert('您没有权限,请联系管理员!', function (index) {
            //         vm.reload();
            //     });
            //     return;
            // }
            vm.showList = false;
            vm.title = "新增";
            vm.inspectionPlan = {
                delFlag: '1',
                endDate: '',
                fulfilDate: '',
                carryDate: new  Date(),
                productionLine: '',
                productionLineName: '',
                workshop: '',
                workshopName: '',
                executive: '',
                planName: '',
                id: '',
                devCode: '',
                devName: '',
                devId: '',
            };
            vm.getTreeShow();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getTreeShow();
            vm.getInfo(id);
       //     console.log(vm.inspectionPlan)
        },
        saveOrUpdate: function (event) {
            var url = vm.inspectionPlan.id == (null || '') ? "../inspectionplan/save" : "../inspectionplan/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.inspectionPlan),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../inspectionplan/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../inspectionplan/info/" + id,
                async: true,
                successCallback: function (r) {
                 //   console.log(r.inspectionPlan)
                    vm.inspectionPlan = r.inspectionPlan;
                    if (r.inspectionPlan.devId){

                    }
                    console.log(r.inspectionPlan)
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getTreeShow: function () {
            //加载分类树
            Ajax.request({
                url: '../sys/user/treeDeptByType/DT_SBJSS',
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#categoryTree"), setting, r.list);
                    ztree.expandAll(true);
                }
            });
        },
        /*        deptTreeShows: function () {
                    openWindow({
                        title: "选择生产线",
                        area: ['300px', '450px'],
                        content: jQuery("#categoryLayer"),
                        btn: ['确定', '取消'],
                        btn1: function (index) {
                            var node = ztree.getSelectedNodes();
                            vm.abnormaData.workshopDept = node[0].parentId;
                            if (node[0].deptType == 'DT_CJKSCX') {
                                //选择上级菜单
                                vm.abnormaData.productionLine = node[0].deptId;
                                vm.abnormaData.productionLineName = node[0].name;
                                console.log(vm.abnormaData);
                                layer.close(index);
                            }
                        }
                    });
                },*/
        //生产线
        deptTreeShow: function () {
            openWindow({
                title: "选择生产线",
                area: ['300px', '450px'],
                content: jQuery("#categoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    vm.inspectionPlan.workshop = node[0].parentId;
                    vm.inspectionPlan.workshopName = node[0].parentName;
                    if (node[0].deptType == 'DT_SBJSS') {
                        // //选择上级菜单
                        vm.inspectionPlan.productionLine = node[0].deptId;
                        vm.inspectionPlan.productionLineName = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        /*选择维护规程*/
        getFollowRecord: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择维护规程',
                content: '../device/devplanselect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                  //  console.log(iframeWin)
                  //  console.log(index)

                    var res = iframeWin.getDeviceInfo();
                    console.log(res)
                /*    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else*/
                    vm.inspectionPlan.devCode='';
                    vm.inspectionPlan.devName='';
                    vm.inspectionPlan.devId='';
                    for(var  i=0;i<res.length;i++){
                    layer.close(index);
                    vm.inspectionPlan.devCode += res[i].devCode+',';
                    vm.inspectionPlan.devName += res[i].devName+',';
                    vm.inspectionPlan.devId += res[i].id+',';
                  //  console.log(vm.inspectionPlan.devCode )
                }
                      /*  layer.close(index);
                        vm.inspectionPlan.devCode = res.devCode;
                        vm.inspectionPlan.devName = res.devName;
                        vm.inspectionPlan.devId = res.id;*/

                }
            })
        },
        /**
         * 分派
         * @param params
         */
        assignmentClick: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            vm.assignmentPlanData.codeId=''
            vm.assignmentPlanData.executive=''
            vm.assignmentPlanData.remark= ''
            if (rows.executiveName != null&&rows.executiveName != '') {
                alert('工单已分派,请勿重复操作!', function (index) {
                    vm.reload();
                });
                return;
            }
            //console.log(rows)
            vm.assignmentPlanData.codeId=rows.id
            vm.assignmentPlanData.name=rows.planName
            $.ajax({
                url: '../sys/user/treeDeptByParent',
                async: true,
                success(value) {
                    vm.executiveList=value.list
                    console.log(value)
                }
            })
            openWindow({
                title: '分派任务',
                content: jQuery("#assignmentPlan"),
                area: ['40%', '50%'],
                btn: ['确定', '取消'],
                btn1: function (index) {
                    $.ajax({
                        url: '../inspectionplan/addSendData',
                        type: 'POST',
                        data: {
                            'id': ids,
                            'codeId': vm.assignmentPlanData.codeId,
                            'sendPerson': vm.assignmentPlanData.executive,
                            'remark': vm.assignmentPlanData.remark
                        },
                        success: function (res) {
                            alert(res.msg, 'function');
                            layer.close(index);
                            vm.data1 = [];
                            $.ajax({
                                url: '../inspectionplan/queryPlanAll',
                                type: 'GET',
                                success: function (res) {
                                    for (var dataList in res.allList) {
                                        if (res.allList.hasOwnProperty(dataList)) {
                                            var data=res.allList[dataList];
                                            data.carryDate = transDate(data.carryDate, 'yyyy-MM-dd hh:mm');
                                            data.fulfilDate = transDate(data.fulfilDate, 'yyyy-MM-dd hh:mm');
                                            vm.data1.push(data);
                                        }
                                    }
                                    vm.reload();
                                }
                            })
                        }
                    })
                }
            })
        },
        sendrecord: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 派工记录',
                content: '../sys/sendrecording.html?ids='+ids
            });
        },
        getdevrecordoption: function () {
            var rows = getSelectedRowData("#jqGrid");
            var ids = rows.id;
            if (ids == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 巡检记录',
                content: '../device/devrecordoption.html?ids='+ids
            });
        },
        showOptionBase:function(v){
            //
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 关联巡检选项',
                content: '../device/InspectionRecord.html?id='+v
            });
        }

    }
});
